#from django.shortcuts import render_to_response
from django.shortcuts import render, HttpResponse
import django.forms as forms
from django.http import HttpResponseRedirect
from django.template import Template, Context
from django.contrib import admin, messages
from django import forms
from django.template import RequestContext

class HostForm(forms.Form):
    HOSTNAME = forms.CharField(label='主机IP或域名（示例：127.0.0.1:8080）', max_length=100)


class data_src_form(forms.Form):
    _selected_action = forms.CharField(widget=forms.MultipleHiddenInput)
    # data_src = forms.ModelChoiceField(SuitCase.objects)

    def update_data_src(modeladmin, request, queryset):
        form = None
        if 'cancel' in request.POST:
            modeladmin.message_user(request, u'已取消')
            return
        elif 'data_src' in request.POST:
            form = modeladmin.data_src_form(request.POST)
        if form.is_valid():
            data_src = form.cleaned_data['data_src']
            for case in queryset:
                case.data_src = data_src
                case.save()
            modeladmin.message_user(request, "%s successfully updated." % queryset.count())
            return HttpResponseRedirect(request.get_full_path())
        else:
            messages.warning(request, u"请选择数据源")
            form = None
        if not form:
            form = modeladmin.data_src_form(initial={'_selected_action': request.POST.getlist(admin.ACTION_CHECKBOX_NAME)})
            return render('batch_update.html',
                                      {'objs': queryset, 'form': form, 'path':request.get_full_path(),
                                       'action': 'update_data_src', 'title': u'批量修改数据源为'},
                                      context_instance=RequestContext(request)
                                      )
    update_data_src.short_description = u'批量修改 数据源'
